package com.zzedu.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.enums.CommonEnums;
import com.zzedu.common.utils.ServletUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzSubjectContent;
import com.zzedu.dto.ZzSubjectContentDTO;
import com.zzedu.params.content.ZzSubjectContentParams;
import com.zzedu.service.IZzSubjectContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 题目内容Controller
 *
 * @author Mr.Wang
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/course/content")
public class ZzSubjectContentController extends BaseController {
    @Autowired
    private IZzSubjectContentService zzSubjectContentService;

    /**
     * 查询题目内容列表
     */
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzSubjectContentDTO zzSubjectContent) {
        return getDataTable(zzSubjectContentService.page(zzSubjectContent.getPageDTO(), zzSubjectContentService.buildQuery(zzSubjectContent)));
    }

    /**
     * 导出题目内容列表
     */
    @Log(title = "题目内容", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(ZzSubjectContentDTO zzSubjectContent) {
        List<ZzSubjectContent> list = zzSubjectContentService.list(zzSubjectContentService.buildQuery(zzSubjectContent));
        ExcelUtil<ZzSubjectContent> util = new ExcelUtil<ZzSubjectContent>(ZzSubjectContent.class);
        util.exportExcel(ServletUtils.getResponse(), list, "题目内容数据");
    }


    /**
     * 新增保存题目内容
     */
    @Log(title = "题目内容", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult addSave(@Validated ZzSubjectContentParams zzSubjectContentParams) throws Exception {
        return AjaxResult.success(zzSubjectContentService.save(zzSubjectContentParams));
    }


    /**
     * 获取题目内容详细信息
     */
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zzSubjectContentService.selectById(id));
    }


    /**
     * 修改保存题目内容
     */
    @Log(title = "题目内容", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult editSave(@Validated ZzSubjectContentParams zzSubjectContentParams) throws Exception {
        return AjaxResult.success(zzSubjectContentService.save(zzSubjectContentParams));
    }

    /**
     * 删除题目内容
     */
    @Log(title = "题目内容", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(zzSubjectContentService.removeBatchByIds(ids));
    }

    /**
     * 题目上线
     */
    @Log(title = "题目内容", businessType = BusinessType.UPDATE)
    @RequestMapping("/online/{ids}")
    public AjaxResult online(@PathVariable List<Long> ids) {
        return toAjax(zzSubjectContentService.update(Wrappers.lambdaUpdate(ZzSubjectContent.class)
                .set(ZzSubjectContent::getState, CommonEnums.YES.getCode())
                .in(ZzSubjectContent::getId, ids)));
    }

    /**
     * 题目下线
     */
    @Log(title = "题目内容", businessType = BusinessType.UPDATE)
    @RequestMapping("/offline/{ids}")
    public AjaxResult offline(@PathVariable List<Long> ids) {
        return toAjax(zzSubjectContentService.update(Wrappers.lambdaUpdate(ZzSubjectContent.class)
                .set(ZzSubjectContent::getState, CommonEnums.NO.getCode())
                .in(ZzSubjectContent::getId, ids)));
    }
}
